﻿using System;
using System.Collections.Generic;
using System.Text;
using YouHP.Model;
using YouHP.BLL;
using YouHP.Utility;

namespace YouHP.Pages.Member
{
    public class PmInbox : YouHP.Pages.BasePage
    {
        protected List<MessageInfo> PmList = new List<MessageInfo>();
        protected int UsedMsgCount;
        protected int MaxMsg = 20;
    
        protected int usedmsgbarwidth;
        protected int unusedmsgbarwidth;
      
        private int _PmCount;
        protected int PmCount {get {return _PmCount;} }
        protected int PageSize=15 ;
        protected int PageCount ;
        protected int PageIndex ;
        protected string PageNumbers ;

        protected override void ShowPage()
        {
            base.ShowPage();


            if (IsPost) HandlePost();
            else HandleGet();

            UsedMsgCount = PmCount;
            usedmsgbarwidth = UsedMsgCount * 100 / MaxMsg;
            unusedmsgbarwidth = 100 - usedmsgbarwidth;

        }

        public void HandlePost()
        {
            string[] Pmitemid = Utils.SplitString(RequestTools.GetFormString("pmitemid"), ",");
            bool succ = true;
            List<int> pmids = new List<int>();

            if (succ)
            {
                if (Pmitemid.Length <= 0) succ = false;
                else if (Pmitemid.Length > PageSize) succ = false;
            }
          
            if (succ)
            {
                foreach (string str in Pmitemid)
                {
                    int tmp = Utils.StrToInt(str, -1);
                    if (tmp <= 0)
                    {
                        succ = false;
                        break;
                    }
                    pmids.Add(tmp);
                }
            }
            

            if (succ)
            {
                MessageBLL.Instance.DeleteMessage(pmids.ToArray());
                this.SetUrl("PmInbox.aspx");
                this.SetMetaRefresh(2, "PmInbox.aspx");
                this.AddMsgLine("删除成功");
            }
            else
            {
                this.AddErrLine("删除失败");
            }


        }
        
        public void HandleGet()
        {
            PageIndex = RequestTools.GetQueryInt("page", 1);
            PmList = MessageBLL.Instance.GetMessageList(PageSize, PageIndex, UserData.Id, 0, -1, out _PmCount);
            PageCount = Utils.GetPageCount(PmCount, PageSize);
        	PageNumbers = Utils.GetPageNumbers(PageIndex, PageCount, "PmInbox.aspx", 8,"page",null);
        }

    }
}
